<template>
  <div class="con-cah">
    <chart :options="pie" :init-options="initOptions" ref="pie" autoresize />
  </div>
</template>
<script>
import ECharts from "@/components/echarts/ECharts.vue";

export default {
  components: {
    chart: ECharts
  },
  props: {
    //[{name:"推荐量",color:"#34243",value:123},
    //{name:"阅读量",color:"#34242",value:123},
    //{name:"转发量",color:"#34243",value:123},
    //{name:"收藏量",color:"#34243",value:123}
    //]
    data: {
      type: Array,
      default: []
    },
    title: {
      type: String,
      default: ""
    }
  },
  watch: {
    data(newVal, oldVue) {
      this.init();
    }
  },
  data() {
    return {
      initOptions: {
        renderer: "canvas"
      },
      pie: {
          // title: {
        //   text: "饼图程序调用高亮示例",
        //   x: "center"
        // },

        tooltip: {//鼠标悬停提示
          show: true,
          trigger: "item",
          formatter: "{a} <br/>{b} : {c} ({d}%)"
        },
         legend: {
          orient: "vertical",
          right: 0,
          textStyle: {
            color: '#fff'
          },
          
          data: [] 
        },
        series: [
          {
            name: this.title,
            type: "pie",
            radius: "80%",
            center: ["50%", "60%"],
            // roseType: 'angle',// 南丁格尔图 
            label: {
              normal: {
                show: false, //显示数据
                formatter: "{b}  {d}% \n {c}",
                color: "#fff",
                // position: "inner", //隐藏标识线 
                textStyle: {
                  fontWeight: 300,
                  fontSize: 12 //文字的字体大小
                }, 
                // formatter: "{d}%"
              //   rich: {
              //     //在 rich 里面，可以自定义富文本样式 
              //     a: {
              //       //a可以理解为变量
              //       color: "#fff", //顶部文本颜色 
              //       fontSize: 10 //文字大小
              //     }
              //   }
              }
            },
            data: [],
            itemStyle: {
              emphasis: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)"
              }
            }
          }
        ]
      }
    };
  },
  methods: {
    init() {
      var list = [];
      for (var item of this.data) {
        this.pie.legend.data.push(item.name)
        list.push({
          value: item.value,
          name: item.name,
          radius: item.radius,
          itemStyle: {
            normal: {
              color: item.color
            }
          }
        });
      }
      this.pie.series[0].data = list;
    }
  },
  mounted() {
    this.init();
  }
};
</script>
<style  scoped>
.con-cah {
  width: 100%;
  height: 100%;
}
</style>